有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

springboot应用程序中的java enable XFrameOptions标头(不含spring security)

安全团队测试了我们的应用程序,发现以下警告:

X-Frame-Options header is not included in the HTTP response to protect against 'ClickJacking' attacks.

我们在应用程序中使用spring boot,但不使用spring安全性。我们使用定制的安全机制

有没有办法将此标题添加到所有回复中


共 (1) 个答案

  1. # 1 楼答案

    您可以创建自定义筛选器并在其中设置标题:

    public class XFrameFilter extends OncePerRequestFilter {
    
        @Override
        protected void doFilterInternal(HttpServletRequest httpRequest,
                                        HttpServletResponse httpResponse,
                                        FilterChain filterChain) throws ServletException, IOException {
            httpResponse.setHeader("X-FRAME-OPTIONS", "DENY");
    
            filterChain.doFilter(httpRequest, httpResponse);
        }
    }